package exhi.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.stp.core.entity.PageInfo;
import com.stp.std.entity.ColumnInfo;
import com.stp.std.entity.ContentInfo;
import com.stp.std.entity.ContentInfoExt;
import com.stp.std.manager.ColumnMng;
import com.stp.std.manager.ContentInfoMng;

import exhi.entity.Exhi;
import exhi.manager.ExhiMng;

@Controller
public class BrowserAct extends BaseAct {

	private static final String PATH_BROWSER_INDEX = "/index";
	private static final String PATH_BROWSER_COLUMNLIST = "/columnlist";
	private static final String PATH_BROWSER_DETAIL = "/detail";
	private static final String PATH_BROWSER_SEARCH = "/searchlist";
	
	@RequestMapping(PATH_BROWSER_INDEX)
	public String index(ModelMap model) {
		model.addAttribute("menus",getColumns());
		List<Exhi> list1 = exhiMng.findByProperty("exhiStatus", "T",
				" id asc", 1, 5);
		List<Exhi> list2 = exhiMng.findByProperty("exhiStatus", "T",
				" id desc", 1, 5);
		String sql=" from ContentInfo  where  adron like '%T%' order by  orderTime desc";
		List<ContentInfo> list3 = infoMng.list(sql);
		model.addAttribute("lstnews1", list1);
		model.addAttribute("lstnews2", list2);
		model.addAttribute("lstnews3", list3);
		return "index";
	}

	@RequestMapping(PATH_BROWSER_COLUMNLIST)
	public String columnList(@RequestParam("code") String code, PageInfo page,
			ModelMap model) {
//		String sql = "from  as info where 1=1 ";
//		sql = sqlModel.getSQLChars("info.code", "=", code, false, "and");
//		PageInfo colPage = infoMng.getPageInfo(sql, page.getPageNo(),
//				page.getPageSize());
//		model.addAttribute("colPage", colPage);
//		ColumnInfo column = columnMng.get(code);
//		if (null != column)
//			model.addAttribute("colName", column.getColumn());
//		model.addAttribute("code", code);
		
		model.addAttribute("menus",getColumns());
		
		List<ContentInfo> list1 = infoMng.findByProperty("code", code,
				" orderTime desc");
		model.addAttribute("columnlist", list1);
		ColumnInfo column = columnMng.get(code);
		if (null != column)
			model.addAttribute("colName", column.getColumn());
		model.addAttribute("code", code);
		return "columnlist";
	}

	// 明细数据
	@RequestMapping(PATH_BROWSER_DETAIL)
	public void infoDetail(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("menus",getColumns());
		ContentInfo contentInfo = infoMng.get(code);
		ContentInfoExt contentInfoExt = infoMng.getInfoExt(code);
		if (null != contentInfo) {
			model.addAttribute("contentInfo", contentInfo);
			ColumnInfo column = columnMng.get(contentInfo.getCode());
			if (null != column)
				model.addAttribute("colName", column.getColumn());
		}
		if (null != contentInfoExt)
			model.addAttribute("contentInfoExt", contentInfoExt);
	}

	
	// 前台页面搜索信息
	@RequestMapping(PATH_BROWSER_SEARCH)
	public void searchList(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@RequestParam(value = "exhiName", required = true, defaultValue = "") String exhiName,
			@RequestParam(value = "exhiCorp", required = true, defaultValue = "") String exhiCorp,		
			@RequestParam(value = "exhiType", required = true, defaultValue = "") String exhiType,
			@RequestParam(value = "exhiAddress", required = true, defaultValue = "") String exhiAddress,
			@RequestParam(value = "exhiDoc", required = true, defaultValue = "") String exhiDoc,
			@RequestParam(value = "exhiYr", required = true, defaultValue = "") String exhiYr,
			@RequestParam(value = "exhiM", required = true, defaultValue = "") String exhiM,
			ModelMap model) {
		model.addAttribute("menus",getColumns());
		String sql = "from Exhi info where info.exhiStatus = 'T' ";
		sql += sqlModel.getSQLChars("info.exhiName", "LIKE", exhiName, false, "and");
		sql += sqlModel.getSQLChars("info.exhiCorp", "LIKE", exhiCorp, false, "and");
		sql += sqlModel.getSQLChars("info.exhiType", "LIKE", exhiType, false, "and");
		sql += sqlModel.getSQLChars("info.exhiAddress", "LIKE", exhiAddress, false, "and");
		sql += sqlModel.getSQLChars("info.exhiDoc", "LIKE", exhiDoc, false, "and");
		sql += sqlModel.getSQLChars("info.exhiYr", "=", exhiYr, false, "and");
		sql += sqlModel.getSQLChars("info.exhiM", "=", exhiM, false, "and");
		sql += " order by info.exhiYr desc, info.exhiM desc limit 20";
		
		List<Exhi> list1 =exhiMng.list(sql);
		model.addAttribute("exhilist", list1);
	}

	protected List<ColumnInfo> getColumns(){
		return columnMng.findByProperty("status","T", " sort ");
	}
		
	@Autowired
	@Qualifier("infoMng")
	private ContentInfoMng infoMng;

	@Autowired
	@Qualifier("columnMng")
	private ColumnMng columnMng;

	@Autowired
	@Qualifier("exhiMng")
	private ExhiMng exhiMng;
}
